import json
import requests
import os


def get_fpath(fname):
    fpath = fname
    if not os.path.exists(fpath):
        fpath = os.path.join('tests', fname)
    return fpath


try:
    # 读入请求路径
    print('------------------- 当前路径: ', os.getcwd(), '-----')
    f_request_url = get_fpath('request_url')
    with open(f_request_url, 'r') as f:
        request_url = f.readline().strip()

    print(f'----------- pytest的请求接口request_url为: {request_url}')
except:
    print('======== url读取失败 ==========')


def upload_file(url, file):

    """

    :param url: 接口url

    :param file: 上传文件的路径

    :return:

    """

    files = {"file": open(file, "rb")}

    s = requests.session()

    r = s.post(url, files=files, verify=False)
    # r.text
    # print(r.json())
    return r.json()


def show_ls(ls):
    assert isinstance(ls, list), 'input must be a list!'
    for d in ls:
        print(d)


def save_list_to_file(ls:list, fname:str):
    file = open(fname, 'w')

    file.write(str(ls))

    file.close()
    print('保存为: ', fname, '长度:', len(ls))
